package com.project.web.course.service.impl.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.common.core.utils.id.IDUtil;
import com.project.web.course.mapper.app.CourseCategoryMapper;
import com.project.web.course.model.app.CourseCategory;
import com.project.web.course.service.app.CourseCategoryService;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 课程类目业务类
 *
 * @Author: wyy
 * @Date: 2020-04-14 16:00
 */
@Service("courseCategoryServiceImpl")
public class CourseCategoryServiceImpl implements CourseCategoryService {

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    /**
     * @param userId 主键
     * @return
     */
    @Override
    public int delete(String userId) {
        if (StringUtils.isBlank(userId)) {
            return 0;
        }
        return courseCategoryMapper.delete(userId);
    }

    /**
     * @param courseCategory 对象
     * @return
     */
    @Transactional
    @Override
    public CourseCategory save(CourseCategory courseCategory) {
        // 根据类父级类目ID查询类目数据
        String parentId = courseCategory.getParentId();
        if (StringUtils.isBlank(parentId)) {
            courseCategory.setTreeLevel(1);
        } else {
            CourseCategory categoryOfParent = courseCategoryMapper.getById(parentId);
            courseCategory.setTreeLevel(categoryOfParent.getTreeLevel() + 1);
            courseCategory.setIsspecial(categoryOfParent.getIsspecial());
        }

        // 设置课程类目ID
        courseCategory.setCreatetime(new Date());
        courseCategory.setCategoryid(String.valueOf(IDUtil.getId()));
        courseCategoryMapper.save(courseCategory);
        return courseCategory;
    }

    /**
     * @param userId 主键
     * @return
     */
    @Override
    public CourseCategory getById(String userId) {

        if (StringUtils.isBlank(userId)) {
            return null;
        }
        return courseCategoryMapper.getById(userId);
    }

    /**
     * @param courseCategory 课程类目对象
     * @return
     */
    @Transactional
    @Override
    public CourseCategory update(CourseCategory courseCategory) {
        // 根据类父级类目ID查询类目数据
        String newPid = courseCategory.getParentId();
        if (StringUtils.isBlank(newPid)) {// 父级类目空
            courseCategory.setTreeLevel(1);
        }

        // 父级不为空且与原父级不相同
        CourseCategory category = courseCategoryMapper.getById(courseCategory.getCategoryid());
        String dbPid = category.getParentId();
        if (StringUtils.isNotBlank(newPid) && !StringUtils.equals(newPid, dbPid)) {
            CourseCategory categoryOfParent = courseCategoryMapper.getById(newPid);
            courseCategory.setTreeLevel(categoryOfParent.getTreeLevel() + 1);
            courseCategory.setIsspecial(categoryOfParent.getIsspecial());
        }

        //  更新课程类目信息
        courseCategoryMapper.update(courseCategory);
        return courseCategory;
    }

    /**
     * @param courseCategory 对象
     * @param pageInfo       分页对象
     * @return
     */
    @Override
    public PageInfo<CourseCategory> selectList(CourseCategory courseCategory, PageInfo<CourseCategory> pageInfo) {
        if (pageInfo != null) {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        }
        return new PageInfo(courseCategoryMapper.selectList(courseCategory));
    }

    /**
     * @param courseCategory 对象
     * @return
     */
    @Override
    public List<CourseCategory> selectList(CourseCategory courseCategory) {
        return courseCategoryMapper.selectList(courseCategory);
    }

    /**
     * 获取所有节点
     *
     * @return
     */
    public List<CourseCategory> getAll() {
        return courseCategoryMapper.getAll();
    }
}
